// 封装分类
import { getCategoryAPI } from "@/apis/category"
import { onMounted, ref } from "vue"
import { useRoute } from "vue-router"
import { onBeforeRouteUpdate } from "vue-router"
export function useCategory () {
  const categoryData = ref({})
  const router = useRoute()
  const getCategory = async (id = router.params.id) => {
    const res = await getCategoryAPI(id)
    categoryData.value = res.result
  }
  onMounted(() => getCategory())
  // 监听路由变化
  onBeforeRouteUpdate((to) => {
    getCategory(to.params.id)
  })
  return {
    categoryData
  }
}